#include "BSP_pwm.h"


static TIM_TypeDef* Target_TIM[5] = {TIME1,TIME2,TIME3,TIME4,TIME5};

static __IO uint16_t* PWM_Channel[13] = {
    &TIME1->CCR1,&TIME1->CCR4,
    &TIME2->CCR1,&TIME2->CCR2,
    &TIME3->CCR1,&TIME3->CCR2,&TIME3->CCR3,&TIME3->CCR4,
    &TIME4->CCR3,&TIME4->CCR4,
    &TIME5->CCR1,&TIME5->CCR2,&TIME5->CCR3
};

static void PWM_GPIO_Init(void);
static void PWM_TIM_Init(void);
static void PWM_Ch_Init(void);

/*******************************************************************
 * 功能:PWM输出初始化
 * 参数:无
 * 返回值:无
 * 2021/5  庞碧璋
 *******************************************************************/
void BSP_PWM_Init(void)
{
    uint8_t temp;
    PWM_GPIO_Init();
    PWM_TIM_Init();
    PWM_Ch_Init();
    //开启4 2定时器
    for( temp = 0;temp<5;temp++)
        TIM_Cmd(Target_TIM[temp],ENABLE);
    //开启TIM 1 8高级定时器的PWM输出
    TIM_CtrlPWMOutputs(TIM1,ENABLE);
    TIM_CtrlPWMOutputs(TIM8,ENABLE);
}

void PWM_GPIO_Init(void)
{
    GPIO_InitTypeDef    GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7|GPIO_Pin_6;
    GPIO_Init(GPIOC,&GPIO_InitStruct);
    
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9|GPIO_Pin_0|GPIO_Pin_1;
    GPIO_Init(GPIOB,&GPIO_InitStruct);
    
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_11;
    GPIO_Init(GPIOA,&GPIO_InitStruct);
}

void PWM_TIM_Init(void)
{
    TIM_TimeBaseInitTypeDef TIMBase_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

    //TIME1 TIM8 50Hz -> 电调 舵机
    TIMBase_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIMBase_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIMBase_InitStruct.TIM_Period = 2500 - 1;
    TIMBase_InitStruct.TIM_Prescaler = 72 -1 ;
    TIMBase_InitStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIME1,&TIMBase_InitStruct);
    TIM_TimeBaseInit(TIME2,&TIMBase_InitStruct);
    TIM_TimeBaseInit(TIME3,&TIMBase_InitStruct);
    TIM_TimeBaseInit(TIME4,&TIMBase_InitStruct);
    TIM_TimeBaseInit(TIME5,&TIMBase_InitStruct);
}

void PWM_Ch_Init(void)
{
    TIM_OCInitTypeDef   TIM_OCInitStruct;

    TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCIdleState_Reset;      //互补通道空闲电平
    TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_Low;         //互补通道有效电平
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;          //有效电平
    TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;   //不开启互补通道
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;

    //TIME1 电调
    TIM_OCInitStruct.TIM_Pulse = 1 - 1;   //中位油门
    TIM_OC1Init(TIME1,&TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIME1,TIM_OCPreload_Enable);
    TIM_OC2Init(TIME1,&TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIME1,TIM_OCPreload_Enable);
    TIM_OC3Init(TIME1,&TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIME1,TIM_OCPreload_Enable);
    TIM_OC4Init(TIME1,&TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIME1,TIM_OCPreload_Enable);

    //TIME2 舵机
    TIM_OCInitStruct.TIM_Pulse = 1 - 1;  //中位
    TIM_OC1Init(TIME2,&TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIME2,TIM_OCPreload_Enable);
    TIM_OC2Init(TIME2,&TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIME2,TIM_OCPreload_Enable);
    TIM_OC3Init(TIME2,&TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIME2,TIM_OCPreload_Enable);
    TIM_OC4Init(TIME2,&TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIME2,TIM_OCPreload_Enable);


    TIM_OCInitStruct.TIM_Pulse = 1 - 1;  //中位
    TIM_OC1Init(TIME3,&TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIME3,TIM_OCPreload_Enable);
    TIM_OC2Init(TIME3,&TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIME3,TIM_OCPreload_Enable);
    TIM_OC3Init(TIME3,&TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIME3,TIM_OCPreload_Enable);
    TIM_OC4Init(TIME3,&TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIME3,TIM_OCPreload_Enable);

    TIM_OCInitStruct.TIM_Pulse = 1 - 1;  //中位
    TIM_OC1Init(TIME4,&TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIME4,TIM_OCPreload_Enable);
    TIM_OC2Init(TIME4,&TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIME4,TIM_OCPreload_Enable);
    TIM_OC3Init(TIME4,&TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIME4,TIM_OCPreload_Enable);
    TIM_OC4Init(TIME4,&TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIME4,TIM_OCPreload_Enable);

    TIM_OCInitStruct.TIM_Pulse = 1 - 1;  //中位
    TIM_OC1Init(TIME5,&TIM_OCInitStruct);
    TIM_OC1PreloadConfig(TIME5,TIM_OCPreload_Enable);
    TIM_OC2Init(TIME5,&TIM_OCInitStruct);
    TIM_OC2PreloadConfig(TIME5,TIM_OCPreload_Enable);
    TIM_OC3Init(TIME5,&TIM_OCInitStruct);
    TIM_OC3PreloadConfig(TIME5,TIM_OCPreload_Enable);
    TIM_OC4Init(TIME5,&TIM_OCInitStruct);
    TIM_OC4PreloadConfig(TIME5,TIM_OCPreload_Enable);
    
    
}

/*******************************************************************
 * 功能:PWM输出
 * 参数:
 *  Channel:PWM通道 -> 数组PWM_Channel[]标号
 *  CCR:通道的比较寄存器的值 -> 用于设置脉宽/占空比
 * 返回值:无
 * 2021/5  庞碧璋
 *******************************************************************/
void PWM_Out(uint8_t Channel,uint16_t CCR)
{
    if(Channel < 13)
    *PWM_Channel[Channel] = CCR;
}

/*******************************************************************
 * 功能:读取当前PWM输出
 * 参数:PWM通道 -> 数组PWM_Channel[]标号
 * 返回值:通道的比较寄存器的值
 * 2021/5  庞碧璋
 *******************************************************************/
uint16_t PWM_Read(uint8_t Channel)
{
    if(Channel < 12)
        return *PWM_Channel[Channel];
    return 0xffff;
}
